﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _1Dot9MasterCommon;
using System.Collections;
using _1Dot9MasterBiz;

public partial class Admin_Popup_AdminPwModify : AdminBasePage
{
    private string strUserId = "";
    private string strUserPw = "";
    private string strUserPwCk = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        init();
    }

    protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script>parent.ClosePopup();</script>");
    }

    protected void ibtnConfirm_Click(object sender, ImageClickEventArgs e)
    {
        
        strUserId = this.txtId.Text.Trim();
        strUserPw = this.txtPw.Text.Trim();
        strUserPwCk = this.txtPwCk.Text.Trim();

        if (!SaveValidation())
            return;

        if (AdminUserPwModify() < 1)
        {
            Response.Write("<script> alert('신규 관리자 등록 실패!'); </script>");
            return;
        }

        Response.Write("<script>parent.ClosePopup();</script>");
    }

    protected bool SaveValidation()
    {
        if (string.IsNullOrEmpty(strUserPw))
        {
            Response.Write("<script> alert('[Password]를 입력하세요'); </script>");
            this.txtPw.Focus();
            return false;
        }

        if (string.IsNullOrEmpty(strUserPwCk))
        {
            Response.Write("<script> alert('[Password(확인)]를 입력하세요'); </script>");
            this.txtPwCk.Focus();
            return false;
        }

        if (!strUserPw.Equals(strUserPwCk))
        {
            Response.Write("<script> alert('[Password]와 [Password(확인)] 이 서로 다릅니다. 다시 입력하세요'); </script>");
            this.txtPw.Focus();
            return false;
        }

        return true;
    }

    /// <summary>
    /// 신규 어드민 유저 등록
    /// </summary>
    protected int AdminUserPwModify()
    {
        int result = 0;
        try
        {
            CryptoUtil crypto = new CryptoUtil();
            string encryptoPw = crypto.AESEncrypt256(strUserPw);

            using (AdminBiz adminBiz = new AdminBiz())
            {
                Hashtable htParams = new Hashtable();

                htParams.Add("@P_USER_ID", strUserId);
                htParams.Add("@P_USER_PW", encryptoPw);
                
                result = adminBiz.SetAdminUserPw(htParams);
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script> alert('" + ex.Message.ToString() + "'); </script>");
        }

        return result;
    }
}